#ifdef U8x8lib
#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
//U8X8_SSD1306_128X64_NONAME_4W_SW_SPI u8x8(U8G2_R0, /* clock=*/ D6, /* data=*/ D5, /* cs=*/ D4, /* dc=*/ D8, /* reset=*/ D0);
//U8X8_SSD1306_128X64_NONAME_4W_SW_SPI u8x8(/* clock=*/ D6, /* data=*/ D5, /* cs=*/ D4, /* dc=*/ D8, /* reset=*/ D0);
#define U8LOG_WIDTH 16
#define U8LOG_HEIGHT 8
uint8_t u8log_buffer[U8LOG_WIDTH * U8LOG_HEIGHT];
U8X8LOG u8x8log;
unsigned long t = 0;
/*****************************flipmode********************************/
void initu8x8code()
{
  u8x8.begin();
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  u8x8log.setRedrawMode(1);    // 0: Update screen with newline, 1: Update screen for every char
  u8x8log.print("millis=");
  u8x8log.print("\n");
}
void U8x8lib_drawr_als_wdata(int ch0, int ch1, uint8_t current_stage)

{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8log.print("ALS=");
  u8x8log.print(ch0);
  //u8x8log.print("\n");
  //u8x8log.print("ch1=");
  u8x8log.print(" ");
  u8x8log.print(ch1);
  //u8x8log.print("\n");
  //u8x8log.print("ag=");
  u8x8log.print(" ");
  u8x8log.print(current_stage);
  u8x8log.print("\n");
}
void U8x8lib_drawr_ps_wdata(int ch0, int ch1, uint8_t current_stage)

{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8log.print("PS=");
  u8x8log.print(ch0);
  u8x8log.print(" ");
  //u8x8log.print("ch1=");
  u8x8log.print(ch1);
  //u8x8log.print("\n");
  //u8x8log.print("st=");
  u8x8log.print(" ");
  u8x8log.print(current_stage);
  u8x8log.print("\n");
}
void U8x8lib_drawr_data(int ch0, int ch1, uint8_t current_stage)

{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.drawString(0, 1, "Hello World!");

  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);
  u8x8.drawString(0, 0, "ABC defg");

  u8x8.setFont(u8x8_font_px437wyse700b_2x2_r);
  u8x8.drawString(0, 2, "ABC defg");
}
void pre(void)
{
  // Serial.println("pre=");
  u8x8.setFont(u8x8_font_amstrad_cpc_extended_f);
  u8x8.clear();
  u8x8.inverse();
  u8x8.print(" alsps show ");
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.noInverse();
  u8x8.setCursor(0, 1);
}
void draw_bar(uint8_t c, uint8_t is_inverse)
{
  // Serial.println("draw_bar=");
  uint8_t r;
  u8x8.setInverseFont(is_inverse);
  for ( r = 0; r < u8x8.getRows(); r++ )
  {
    u8x8.setCursor(c, r);
    u8x8.print(" ");
  }
}

void draw_ascii_row(uint8_t r, int start)
{
  //   Serial.println("draw_ascii_row=");
  int a;
  uint8_t c;
  for ( c = 0; c < u8x8.getCols(); c++ )
  {
    u8x8.setCursor(c, r);
    a = start + c;
    if ( a <= 255 )
      u8x8.write(a);
  }
}
void U8x8lib_draw_alspsdata(int ch0, int ch1, uint8_t current_stage)
{
  //pre();
  //u8x8.drawString(0, 1, "3x6 Font");
  //u8x8.setFont(u8x8_font_inb33_3x6_n);
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.setCursor(0, 1);
  u8x8.print("ch0=");
  u8x8.setCursor(3, 1);
  u8x8.print(ch0);
  u8x8.print("  ");
  u8x8.setCursor(0, 2);
  u8x8.print("ch1=");
  u8x8.setCursor(3, 2);
  u8x8.print(ch1);
  u8x8.setCursor(0, 3);
  u8x8.print("AG=");
  u8x8.setCursor(3, 3);
  u8x8.print(current_stage);
  // u8x8.setCursor(0, 2);
  //u8x8.print(ch0);      // Arduino Print function
  // u8x8.draw2x2String(0, 5, ch0);
  // u8x8.setCursor(0, 4);
  // u8x8.print(ch1);      // Arduino Print function
  // u8x8.setCursor(0, 6);
  // u8x8.print(current_stage);      // Arduino Print function
  // delay(10);

  // u8x8.drawString(0, 2, u8x8_u16toa(i, 5)); // U8g2 Build-In functions
  //delay(10);

}

void dublog()
{
  u8x8log.print("\f");
  u8x8log.print("ch0=");
  u8x8log.print(mn_sensor_als_data_channel0);
  u8x8log.print("\n");
  u8x8log.print("ch1=");
  u8x8log.print(mn_sensor_als_data_channel1);
  u8x8log.print("\n");
  u8x8log.print("0x69=");
  u8x8log.print(mn_sensor_als_ag_current_stage, HEX);
  u8x8log.print("\n");
}

#endif
